package checkers3d.logic;

import checkers3d.presentation.*;
import checkers3d.storage.DataManagerGame;
import checkers3d.storage.DataManagerInstantReplay;
import checkers3d.storage.DataManagerUtil;
import java.awt.Point;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ruben
 */
public class GameMenu extends GUIContainer
{
    GameMenu(Game game,Player home, Player visitor, Player current, PlayingSurface3D surface, LinkedList<PlayingSurface3D> replays, Point size)
    {
        super(size);

        //add background to stop overdraw.
        add(new GUIPictureBox("Background.png", new Point()));

        LinkedList<IDrawable> buttons = new LinkedList<IDrawable>();

        //add the start button with behavior.
        buttons.add(new GUIButton("button-return.png", null, new ObserverReturn()));

        //add the start button with behavior.
        buttons.add(new GUIButton("button-replay.png", null, new ObserverReplay(home, visitor, replays)));

        //add the start button with behavior.
        buttons.add(new GUIButton("button-save.png", null, new ObserverSave(current, home, visitor, surface, replays)));

        //add the concede button with behavior.
        buttons.add(new GUIButton("button-concede.png", null, new ObserverConcede(game, current, home, visitor)));

        //add the draw button with behavior.
        buttons.add(new GUIButton("button-game-draw.png", null, new ObserverDraw(home, visitor, getSize())));

        //add the exit button with behavior.
        buttons.add(new GUIButton("button-exit.png", null, new ObserverExit()));

        for(IDrawable button :  buttons)
            add(button);

        UtilGUI.computeLayoutCentered(getSize(), getSize().x / 2, buttons);
    }
    
    @Override
    public void onKeyPress(char key)
    {
        if((int)key == UtilGUI.KEY_ESCAPE);
            GameMaster.getInstance().removeTopGUIContainer();
    }

    private class ObserverReturn implements IInputObserver
    {
        public void onClick(Point position)
        {
            GameMaster.getInstance().removeTopGUIContainer();
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverReplay implements IInputObserver
    {
        LinkedList<PlayingSurface3D> replays;
        Player home;
        Player visitor;

        public ObserverReplay(Player home, Player visitor, LinkedList<PlayingSurface3D> replays)
        {
            this.replays = replays;
            this.home = home;
            this.visitor = visitor;
        }

        public void onClick(Point position)
        {
            GameMaster.getInstance().addGUIContainer(new InstantReplayWindow(home, visitor, replays, getSize()));
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverSave implements IInputObserver
    {
        /*
         * Stores instant replay data.
         */
        private DataManagerInstantReplay instantReplayDataManager = new DataManagerInstantReplay();

        PlayingSurface3D playingSurface3D;

        Player current;
        Player home;
        Player visitor;

        LinkedList<PlayingSurface3D> replays;

        public ObserverSave(Player current, Player home, Player visitor, PlayingSurface3D surface, LinkedList<PlayingSurface3D> replays)
        {
            this.current = current;
            this.home = home;
            this.visitor = visitor;
            this.playingSurface3D = surface;
            this.replays = replays;
            System.out.println("\nCurrent player: "
                    + this.current.getDataAsString()
                    + "\nHome player: "
                    + this.home.getDataAsString()
                    + "\nVisitor player: "
                    + this.visitor.getDataAsString() + " ");
        }

        public void onClick(Point position)
        {
            String filenameGame = (DataManagerGame.LOCATION_SAVEGAMES
                    + home.getDataAsString() + "&"
                    + visitor.getDataAsString() + ".txt");
            String filenameReplay = (DataManagerInstantReplay.LOCATION_SAVEREPLAYS
                    + home.getDataAsString() + "-"
                    + visitor.getDataAsString() + "-"
                    + "REPLAY.txt");

            try
            {
                // Erases the current game file.
                DataManagerUtil.eraseFile(filenameGame);
                // Saves the current game file.
                DataManagerGame.saveGameData(playingSurface3D,
                                             current.getDataAsString(),
                                             home.getDataAsString(),
                                             visitor.getDataAsString());

                DataManagerInstantReplay.eraseInstantReplayData(filenameReplay);

                for(PlayingSurface3D replay : replays)
                    DataManagerInstantReplay.saveInstantReplayData(home.getDataAsString(), visitor.getDataAsString(), replay);

                JOptionPane.showMessageDialog(null, "The game has been saved.");
            }
            catch (Exception ex)
            {
                JOptionPane.showMessageDialog(null, "ERROR, the game was not saved.");
                Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverConcede implements IInputObserver
    {
        Player current;
        Player home;
        Player visitor;
        Game game;

        public ObserverConcede(Game game, Player current, Player home, Player visitor)
        {
            this.current = current;
            this.home = home;
            this.visitor = visitor;
            this.game = game;
        }

        public void onClick(Point position)
        {
            if (current == home)
                    game.setWinner(visitor);
            else
                    game.setWinner(home);

            GameMaster.getInstance().removeTopGUIContainer();
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverDraw implements IInputObserver
    {
        Player home;
        Player visitor;
        Point windowSize;

        public ObserverDraw(Player home, Player visitor, Point windowSize)
        {
            this.home = home;
            this.visitor = visitor;
            this.windowSize = windowSize;
        }

        public void onClick(Point position)
        {
            GameMaster.getInstance().removeTopGUIContainer();
            GameMaster.getInstance().addGUIContainer(new GameMenuDrawLogin(windowSize));
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverExit implements IInputObserver
    {
        public void onClick(Point position)
        {
            //remove this menu
            GameMaster.getInstance().removeTopGUIContainer();

            //.remove the game
            GameMaster.getInstance().removeTopGUIContainer();
        }

        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }
}
